#ifndef BSP_CONFIG_H
#define BSP_CONFIG_H

#include <stdint.h>
#include "hem1601.h"
#define ARRAY_SZ(a) (sizeof(a) / sizeof(a[0]))
#define Min(a, b) (((a) < (b)) ? (a) : (b))
#define Max(a, b) (((a) > (b)) ? (a) : (b))

#define FLASH_CONFIG_OK 0xaa55
/**
 * @brief 恒流端口：PWMi8-Q08， PWMi9-Q09， PWMi10-Q10，
 *                  PWMi11-Q11，PWMi12-Q12，PWMi13-Q13
 */
#define HEM_PWMI_NUM 6 //恒流输出，只有6路

enum IN_MODE {
  IN_MODE_5V = 0u,
  IN_MODE_36V,
  IN_MODE_20MA,
  IN_MODE_30K,
  IN_MODE_DIH,
  IN_MODE_DIL
};
/**
 * @brief 0:input;  1:输出高边;2：输出PWM；3：输出全桥；4：输出底边；只有8路端口能够配置成输入或者输出
 */
typedef enum port_mode
{
  eInput = 0u,
  eHighSideOut,
  ePWM,
  eFullBridge,
  eLowSideOut,
  ePortModeNumber
}port_mode_t;

typedef struct hem_config 
{
  uint32_t config_ok;
    
  uint32_t node_type;
  uint32_t node_id;
  uint32_t baudrate;
  
  uint32_t up_cycle_max[(HEM_AI_NUM + 3) / 4 + 1];
  uint32_t up_cycle_min[(HEM_AI_NUM + 3) / 4 + 1];
  uint32_t err_interval;
  uint32_t do_timeout_ms;
  uint32_t pwm_timeout_ms;
  uint32_t pwmi_timeout_ms;
  uint32_t ao_timeout_ms;
  uint32_t port_mode[8];   //0:input;  1:输出高边;2：输出PWM；3：输出全桥；4：输出底边；只有8路端口能够配置成输入或者输出
#if HEM_AI_NUM > 0
  uint32_t in_mode[10];//只有10个端口能够配置成输入，且可以选择电压，电流等采样
  uint32_t ai_deadzone;
#endif
#if HEM_PWM_NUM >0
  uint32_t out_mode[HEM_PWM_NUM]; //0:DO; 1:pwm;2:pwmi;
  uint32_t pwm_freq[HEM_PWM_FREQ_NUM];
#endif  
#if HEM_PWMI_NUM > 0
  uint32_t pwmi_kp100;
  uint32_t pwmi_ki100;
  uint32_t pwmi_kd100;
#endif
  
} hem_config_t;

extern int32_t hem_config_load(hem_config_t *p_config);
extern int32_t hem_config_save(hem_config_t *p_config);
extern void node_config_init(hem_config_t *config);
#endif
